; Adjust Brush Position Macro
; This macro allows the user to adjust the Y center position of the cleaning brush

; Determine the current value to display as default
var currentValue = -62
if global.brushYPosition != null
  set var.currentValue = floor(global.brushYPosition + 0.5)

; Prompt user to enter new position value (S5 = integer input, L = min, H = max, F = default, J1 = cancel button terminates macro)
M291 R"Adjust Brush Position" P"Please adjust the brush Y center position (-100 to -50 mm)" S5 L-100 H-50 F{var.currentValue} J1

; Store the user input (convert to float for consistency with global variable)
var newPosition = 0.0 + input

; Validate the input is within range (double check, though M291 should enforce it)
if var.newPosition < -100.0 || var.newPosition > -50.0
  M98 P"0:/sys/led/fault.g"  ; activate red LEDs for error
  echo >>"0:/sys/eventlog.txt" "Error: brushYPosition value "^var.newPosition^" is out of range (-100 to -50)"
  M291 R"Error" P{"Value " ^ var.newPosition ^ " is out of range (-100 to -50)"} S1 T10
  abort "Error: brushYPosition value is out of range (-100 to -50)"

; Save the new value to the file
echo >"0:/sys/user/variables/BrushYPosition.g" "set global.brushYPosition = "^var.newPosition

; Load the new value immediately
M98 P"0:/sys/user/variables/BrushYPosition.g"

; Confirm to user (S1 = close button, T5 = 5 second timeout)
M291 R"Success" P{"Brush Y center position updated to " ^ var.newPosition ^ " mm"} S1 T5
